<?php

namespace app\common\traits;

use app\admin\model\order\Movie as MovieModel;
use app\common\facade\auth\Auth;
use app\common\service\around\JtkAroundService;
use app\common\service\auth\AuthApiService;
use app\common\service\order\CouponOrderService;
use app\model\CouponOrder;
use app\model\MovieOrder as MovieOrderModel;
use app\ymsh\model\Order as OrderModel;
use think\Exception;
use think\Request;

trait Order
{
    /**
     * 统一订单
     */
    public function unionList()
    {
        $params = request()->all();
        $model = new OrderModel();
        $data = $model->unionList($params);

        return success('', $data['data'] ?? []);

    }


    /**
     * 创建订单
     */
    public function create()
    {

        $params = request()->all();

        $auth = Auth::instance();

        try {

        if ($auth instanceof AuthApiService) {

            $couponOrder = (new CouponOrderService())->create($params);

        } else {
            throw new Exception('该渠道暂不支持创建订单');
        }
        $couponOrder->append(['expired_time']);

        return success('', $couponOrder);

        } catch (Exception $exception) {

            return fail($exception->getMessage());

        }


    }

    /**
     * 详情
     */
    public function detail()
    {
        $code = input('code');
        $id = input('id');
        $order_id = input('order_id');
        if (empty($id) && empty($order_id) && empty($code) ) {
            return fail('参数id和order_id不能同时为空');
        }


        $servType = input('serv_type'); //
        $append = ['expired_time','return_status', 'order_status_text', 'refundBtnStatus', 'refundFailTip'];
        if ($servType != PRODUCT_AROUND_SHOP_SERV) {
            $append[] = 'couponProductChannel';
        }
//        $extraField = input('extra_field'); // 多个用逗号隔开
//        if ($extraField) {
//            $extraField = explode(',', $extraField);
//            $append = array_merge($append, $extraField);
//        }
        $res = CouponOrder::query()
            ->with(['couponBrand'])
            ->when($id, function ($query) use ($id) {
                $query->where('id', $id);
            })->when($order_id, function ($query) use ($order_id) {
                $query->where('order_id', $order_id);
            })->when($code,function ($query)use($code){
                $query->where('id',alphaID($code));
            })->find()->append($append);
        if ($res && !in_array($res['serv_type'], [PRODUCT_FILM_SERV, PRODUCT_AROUND_SHOP_SERV])) {
            $res['ext_data'] = '';
        }

        if ($res['serv_type'] == PRODUCT_AROUND_SHOP_SERV) {
            // 周边游 前端核销参数需要额外处理 处理成订单ID数组
            $extData = $res['ext_data'] ?? [];
            $orderNotifyCheck = $extData[JtkAroundService::ORDER_NOTIFY_CHECK] ?? [];
            if ($orderNotifyCheck) {
                $key = JtkAroundService::ORDER_NOTIFY_CHECK . 'Arr';
                $extData[$key] = Collect($orderNotifyCheck)->column('orderId');
                $res['ext_data'] = $extData;
            }
        }
        return success('', $res);

    }


    /**
     * 电影订单
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function movie()
    {
        if (\app\common\facade\auth\Auth::isAdmin()) {
            $model = new MovieModel;
            $data = $model->getList(input());
            return $data;
        }
    }

    /**
     * 根据订单号查询对应的电影订单详情
     * @author ymzhang
     * @access public
     * @date 2022-08-23 14:10
     */
    public function movieOrderDetail(Request $request)
    {
        $params = $request->param();
        if (empty($params['id']) && empty($params['order_id'])) {
            return fail('参数id和order_id不能同时为空');
        }
        $id = $params['id']??null;
        $order_id = $params['order_id']??null;
        $res = MovieOrderModel::query()
            ->when($id, function ($query) use ($id) {
                $query->where('id', $id);
            })->when($order_id, function ($query) use ($order_id) {
                $query->where('order_id', $order_id);
            })->with(['couponOrder'])->find();
        return success('success', $res);
    }

}